﻿using System.Collections.Generic;
using System.Windows;

namespace Red.Launcher.ViewModels.Workspaces.Database
{
    public class P4ClientWorkspace : Record
    {
        #region Private Data

        private string _perforceName;

        #endregion Private Data

        #region Public Properties

        public static Metadata METADATA { get; private set; }

        public string PerforceName
        {
            get { return _perforceName; }
            set
            {
                if ( _perforceName != value )
                {
                    _perforceName = value;
                    OnPropertyChanged();
                }
            }
        }

        #endregion Public Properties

        #region Implemented Properties

        public override string Icon { get { return "appbar_lamp_desk_variant"; } }
        public override IEnumerable<object> Values { get { return new List<object>() { Name, PerforceName }; } }
        public override Metadata Metadata { get { return METADATA; } }

        #endregion Implemented Properties

        #region Overridden Methods

        internal override bool AcceptsDraggedData( IDataObject data )
        {
            return data.GetDataPresent( typeof( ClientParameter ) );
        }

        internal override void PerformDrop( IDataObject data )
        {
            DraggedRecord dragged = data.GetData( typeof( ClientParameter ) ) as DraggedRecord;

            if ( dragged != null )
            {
                ClientParameter param = dragged.Record as ClientParameter;

                if ( param != null )
                {
                    ClientParameter existing = FindChild( param.Name ) as ClientParameter;

                    if ( existing != null )
                    {
                        existing.Value = param.Value;
                    }
                    else
                    {
                        //Sigh, C#, you can do better than this
                        //You shouldn't need to downcast for a function
                        Record record = new ClientParameter()
                        {
                            Name = param.Name,
                            Value = param.Value
                        };

                        AddChild( ref record );
                    }
                }
            }
        }

        #endregion Overridden Methods

        #region C-Tor

        static P4ClientWorkspace()
        {
            METADATA = new Metadata()
            {
                Table = "P4Client",
                Keys = new List<string>() { "displayname", "p4client_name" },
                CreateDefault = () => { return new P4ClientWorkspace(); },
                CreateFromDatabaseRow = ( Dictionary<string, object> data ) =>
                {
                    return new P4ClientWorkspace()
                    {
                        Id = (int)data[ "id" ],
                        Name = (string)data[ "displayname" ],
                        PerforceName = (string)data[ "p4client_name" ]
                    };
                }
            };

            METADATA.Children.Add( typeof( ClientParameter ) );
        }

        #endregion C-Tor
    }
}
